package com.bruceewu.galleryphoto.base;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.bruceewu.configor.RecyclerViewConfigor;
import com.bruceewu.configor.entity.DisplayItem;
import com.bruceewu.configor.helper.ThreadPool;
import com.bruceewu.configor.helper.ViewHelper;
import com.bruceewu.configor.holder.base.GalleryHolder;
import com.bruceewu.galleryphoto.R;
import com.bruceewu.galleryphoto.bean.ImageBean;
import com.bruceewu.galleryphoto.utis.LogUtils;
import com.bruceewu.galleryphoto.utis.ScreenUtils;
import com.bruceewu.galleryphoto.utis.SharedPreferenceHelper;
import com.bruceewu.galleryphoto.utis.ToastUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class PhotoFragment extends Fragment {
    private IListConfigor configor;

    public static Fragment newInstance(IListConfigor configor) {
        PhotoFragment fragment = new PhotoFragment();
        fragment.configor = configor;
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_photo, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        ViewHelper mHelper = new ViewHelper(view);
        mHelper.setVisibility(R.id.back, SharedPreferenceHelper.getShowBack());
        RecyclerViewConfigor builder = new RecyclerViewConfigor
                .Builder()
                .buildRecyclerView(mHelper.getView(R.id.list))
                .buildScrollType(RecyclerViewConfigor.ScrollType.Vertical)
                .buildRefresh(false)
                .buildLoadMore(false)
                .buildInfinite(false)
                .buildClickListener(item -> {
                    SharedPreferenceHelper.putLastFile(item.showData());
                    ToastUtils.show("播放进度保存成功！");
                })
                .buildBgColor(getResources().getColor(R.color.black))
                .build();
        mHelper.setClick(R.id.back, () -> Objects.requireNonNull(getActivity()).finish());
        boolean showTip = SharedPreferenceHelper.getShowTip();
        mHelper.setInVisibility(R.id.tip, showTip);
        ThreadPool.work(() -> {//耗时操作
            List<DisplayItem> subs = configor.imgs();
            ThreadPool.main(() -> {//UI操作
                if (subs.isEmpty()) {
                    ToastUtils.show("没有找到图片!");
                    Objects.requireNonNull(getActivity()).finish();
                } else {
                    int screenWidth = ScreenUtils.getScreenWidth();
                    int screenHeight = ScreenUtils.getScreenHeight();
                    boolean needScroll = subs.size() > 1;
                    DisplayItem gallery = new GalleryHolder
                            .Builder()
                            .setWidth(screenWidth)
                            .setHeight(screenHeight)
                            .setTime(SharedPreferenceHelper.getTime())
                            .setInfinite(needScroll)
                            .setAutoScroll(needScroll)
                            .setHideIndicator(!SharedPreferenceHelper.getShowIndicator())
                            .setIndicatorType(GalleryHolder.INDICATOR_TYPE_NUM)
                            .setChildren(subs)
                            .setLoopCallBack(posObj -> {
                                try {
                                    if (showTip) {
                                        int pos = (int) posObj;
                                        ImageBean info = (ImageBean) subs.get(pos).getExtra("info");
                                        mHelper.setText(R.id.tip, info.tip());
                                    }
                                } catch (Exception ex) {
                                    LogUtils.logError(ex.getMessage());
                                }
                            })
                            .build();

                    List<DisplayItem> items = new ArrayList<>();
                    items.add(gallery);
                    builder.set(items);
                }
            });
        });
    }
}
